﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Text;
using System.Data.Odbc;
using Reviewer.Entities.Services;
using Reviewer.Business;
using Reviewer.Entities;

public partial class AlaTestFeed : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ClearContent();
        Response.ClearHeaders();
        Response.Clear();

        Response.ContentType = "text/xml";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetAllowResponseInBrowserHistory(false);
        Response.Cache.SetExpires(DateTime.Now);

        XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
        writer.Formatting = Formatting.Indented;
        writer.Indentation = 4;
        writer.WriteStartDocument();
        writer.WriteDocType("alatest", null, "http://feed.alatest.com/infeed.dtd", null);
        writer.WriteStartElement("alatest");
        writer.WriteStartElement("Sources");

        writer.WriteStartElement("Source");
        writer.WriteElementString("Source_Name", "Reviewer.dk");
        writer.WriteElementString("Language", "da-DK");
        writer.WriteElementString("SourceURL", "http://www.reviewer.dk");

        writer.WriteStartElement("Products");


        using (IReviewService reviewService = ServiceFactory.ReviewService)
        using (IGameService gameService = ServiceFactory.GameService)
        using (IMetadataService<Format> formatService = ServiceFactory.GetMetadataService<Format>())
        {
            foreach(Review review in reviewService.Find().OrderByDescending(t => t.Id))
            {
                Game currentGame = gameService.GetBy(review.GameId);
                if (currentGame == null)
                {
                    continue;
                }

                Format gameFormat = formatService.GetAll().FirstOrDefault(t => t.Id.Equals(currentGame.Format));

                writer.WriteStartElement("Product");

                writer.WriteElementString("InternalProductIdentifier", review.Id.ToString());
                writer.WriteElementString("Source_name", "Reviewer.dk");
                writer.WriteElementString("ProductName", currentGame.Title);
                writer.WriteElementString("ProductManufacturer", currentGame.Publisher);
                writer.WriteElementString("OriginalCategoryName", "Review");
                writer.WriteElementString("TestUrl", "http://www.reviewer.dk" + UrlUtillity.GetReviewUri(review));

                writer.WriteStartElement("Product_ids");
                writer.WriteStartElement("Product_id");
                writer.WriteElementString("ID_kind", "GameFormat");
                writer.WriteElementString("ID_value", gameFormat == null ? "Ukendt format" : gameFormat.Name);
                writer.WriteEndElement();
                writer.WriteEndElement();

                writer.WriteEndElement();
            }
        }
        writer.WriteEndDocument();
        writer.Flush();
    }

}
